﻿using DevComponents.DotNetBar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UniRlv.Utility;
using UniRlv.Utility.Extend;

namespace UniRlv.UI.Main
{
    public partial class DialogBase : OfficeForm
    {
        public DialogBase()
        {
            InitializeComponent();
            this.EnableGlass = false;
        }

        private void DialogBase_Load(object sender, EventArgs e)
        {
            WorkSpace.GetInstance().CodeScanEvent +=  CodeScanEventPrc;
        }

        private void CodeScanEventPrc(object sender, CodeScanEventArg e)
        {
            Action<string, string> act = new Action<string, string>((s1, s2) =>
            {
                OnScanCode(s1, s2);
            });

            if (!this.IsDisposed && this.IsHandleCreated)
            {
                try
                {
                    this.Invoke(act, e.codeOrg, e.code24bit);
                }
                catch (Exception)
                {
                }
                
            }
            
        }

        public virtual void OnScanCode(string orgCode, string validCode)
        {

        }

        protected void Wait()
        {
            this.Cursor = Cursors.WaitCursor;
        }

        protected void WaitBack()
        {
            this.Cursor = Cursors.Default;
        }

        #region 消息提示框
        protected void ShowInfoMessageBox(string caption, string msg)
        {
            MessageBox.Show(this, msg, caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        protected void ShowWarnMessageBox(string caption, string msg)
        {
            MessageBox.Show(this, msg, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        public void ShowErrorMessageBox(Exception exp)
        {
            ShowErrorMessageBox("出现异常", exp.Message);
        }

        public void ShowErrorMessageBox(string caption, string info)
        {
            MessageBox.Show(this, info, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        #endregion

        private void DialogBase_FormClosed(object sender, FormClosedEventArgs e)
        {
            WorkSpace.GetInstance().CodeScanEvent -= CodeScanEventPrc;
        }
    }
}
